﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using OTA.Notification.BO.Rule;
using OTA.Notification.Entity.Rule;
using Strive.DataAccess.Base.BO;

namespace OTA.Notification.Web.Rule
{


    public class SendRuleConfigBO : MsgRuleConfigBO
    {

        protected override void ValidationRules()
        {
            //
        }
    }

    public class SendRuleConfigDetailBO : MsgRuleConfigDetailBO
    {

        protected override void ValidationRules()
        {
            //
        }
    }

    public partial class Rule : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();

                if (Request.QueryString["id"] != null)
                {
                    //get info
                    int iID = 0;

                    int.TryParse(Request.QueryString["id"], out iID);

                    BindData(iID);
                }
            }
        }

        private void InitPage()
        {

        }

        private void BindData(int ID)
        {
            MsgRuleConfigBean bean;

            using (SendRuleConfigBO bo = new SendRuleConfigBO())
            {
               bean = bo.Load(ID); // update by kevin 6.25

               if (bean != null)
               {
                   txtName.Text = bean.RuleName;
               }
            }
        }

        private DataTable CreateDataTable()
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("id");
            dt.Columns.Add("type");
            dt.Columns.Add("success");
            dt.Columns.Add("fail");
            dt.Columns.Add("need");

            return dt;
        }

        private DataTable GetDataTableFromPage()
        {

            DataTable dt = CreateDataTable();

            foreach (GridViewRow row in GridView1.Rows)
            {
                DataRow dr = dt.NewRow();

                string sendtype = ((DropDownList)row.Cells[1].FindControl("DropDownList1")).SelectedValue;
                bool success = ((CheckBox)row.Cells[1].FindControl("CheckBox1")).Checked;
                bool fail = ((CheckBox)row.Cells[2].FindControl("CheckBox2")).Checked;
                bool need = ((CheckBox)row.Cells[3].FindControl("CheckBox3")).Checked;

                dr["type"] = sendtype;

                if (success)
                    dr["success"] = true;
                else
                    dr["success"] = false;

                if (fail)
                    dr["fail"] = true;
                else
                    dr["fail"] = false;

                if (need)
                    dr["need"] =true;
                else
                    dr["need"] =false;

                dt.Rows.Add(dr);
            }

            return dt;

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            DataTable dt = GetDataTableFromPage();

            if (dt.Rows.Count > 1)
            {
                dt.Rows.RemoveAt(e.RowIndex);

                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            DataTable dt = GetDataTableFromPage();

            DataRow dr = dt.NewRow();

            dt.Rows.Add(dr);

            GridView1.DataSource = dt;
            GridView1.DataBind();
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtName.Text))
            {
                Response.Write("<script>alert('请输入规则名称！')</script>");
                return;
            }

            using (SendRuleConfigBO bo = new SendRuleConfigBO())
            {

                CreateID();

                int iID = 0;

                MsgRuleConfigBean bean = new MsgRuleConfigBean();

                if (Request.QueryString["id"] != null)
                {
                    

                    int.TryParse(Request.QueryString["id"], out iID);

                    bean = bo.Load(iID);//update by kevin 6.25

                    if (bean == null)
                    {
                        return;
                    }
                }
                else
                    bean.CreateTime = DateTime.Now;

                bean.LastUpdateTime = DateTime.Now;
                bean.MsgTypeCode = ddlMsgType.SelectedValue;
                bean.RuleName = txtName.Text;

                bo.Save(bean);

                //save detail

                using (SendRuleConfigDetailBO detailBO = new SendRuleConfigDetailBO())
                {

                    //delete detail old data
                    detailBO.DeleteAll(iID, bean.MsgTypeCode);

                    //insert detail

                    DataTable dt = GetDataTableFromPage();

                    foreach (DataRow dr in dt.Rows)
                    {
                        MsgRuleDetailConfigBean b = new MsgRuleDetailConfigBean();


                    }
                }
            }


            Response.Write("<script>window.parent.Close()</script>");
        }


        private int CreateID()
        {
            DateTime date = DateTime.Now;

            string strID = date.Ticks.ToString();

            return 0;
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ((CheckBox)e.Row.Cells[1].FindControl("CheckBox1")).CssClass = "chk" + e.Row.RowIndex.ToString();
                ((CheckBox)e.Row.Cells[2].FindControl("CheckBox2")).CssClass = "chk" + e.Row.RowIndex.ToString();

                ((CheckBox)e.Row.Cells[3].FindControl("CheckBox3")).CssClass = "chkneed";

                bool bNeed = false;
                bool bSuccess = false;
                bool bFail = false;

                bool.TryParse(GridView1.DataKeys[e.Row.RowIndex][0].ToString(), out bNeed);

                bool.TryParse(GridView1.DataKeys[e.Row.RowIndex][1].ToString(), out bSuccess);
                bool.TryParse(GridView1.DataKeys[e.Row.RowIndex][2].ToString(), out bFail);

                ((CheckBox)e.Row.Cells[3].FindControl("CheckBox3")).Checked = bNeed;

                ((CheckBox)e.Row.Cells[1].FindControl("CheckBox1")).Checked = bSuccess;
                ((CheckBox)e.Row.Cells[2].FindControl("CheckBox2")).Checked = bFail;

                if (((CheckBox)e.Row.Cells[3].FindControl("CheckBox3")).Checked)
                {
                    ((CheckBox)e.Row.Cells[1].FindControl("CheckBox1")).Enabled = false;
                    ((CheckBox)e.Row.Cells[2].FindControl("CheckBox2")).Enabled = false;
                }
            }
        
        }

    }
}
